home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-GM / MPW / Examples / CFMExamples / ModApp / Menus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-03  |  7.2 KB  |  312 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Menus.c
  3.  
  4.     Contains:    Common code for handling our Application's menus
  5.  
  6.     Written by:    Richard Clark
  7.  
  8.     Copyright:    © 1993, 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.                   11/29/93    RC        Updated AdjustMenus to hilight the Modules menu appropriately 
  13.                   11/28/93    RC        Initial release
  14.  
  15.     To Do:
  16. */
  17.  
  18. #ifndef __MENUS__
  19.     #include <Menus.h>
  20. #endif
  21.  
  22. #ifndef __WINDOWS__
  23.     #include <Windows.h>
  24. #endif
  25.  
  26. #ifndef __DIALOGS__
  27.     #include <Dialogs.h>
  28. #endif
  29.  
  30. #ifndef __TEXTUTILS__
  31.     #include <TextUtils.h>
  32. #endif
  33.  
  34. #include <Menus.h>
  35. #include <Devices.h>
  36. #include <Events.h> 
  37.  
  38. #include "ModApp.h"
  39. #include "Prototypes.h"
  40.  
  41. static void SetItemEnable (MenuHandle theMenu, int16 theItem, Boolean enabled);
  42. static void EnableFileCommands (Boolean userWindow);
  43. static void EnableModuleCommands (Boolean userWindow);
  44. static void DoAppleCmds (int16 theItem);
  45. static void DoFileCmds (int16 theItem);
  46. static void DoToolCmds (int16 theItem);
  47.  
  48. static Handle gCurrentMenuBar = NULL;    // Handle to a tool's menu bar or NULL if the default menus are installed
  49.  
  50. void SetItemEnable (MenuHandle theMenu, int16 theItem, Boolean enabled)
  51. {
  52.     if (enabled)
  53.         EnableItem(theMenu, theItem);
  54.     else
  55.         DisableItem(theMenu, theItem);
  56. } /* SetItemEnable */
  57.  
  58.  
  59. void EnableFileCommands (Boolean userWindow)
  60. {
  61. #pragma unused (userWindow)
  62.     MenuHandle    fileMenu = GetMenuHandle(kFileMenu);
  63.     WindowPtr    inFront = FrontWindow();
  64.  
  65.     SetItemEnable(fileMenu, cNew, !(inFront && (((WindowPeek)inFront)->windowKind == dialogKind)));
  66.     SetItemEnable(fileMenu, cClose, (inFront && (((WindowPeek)inFront)->goAwayFlag)));
  67. } /* EnableFileCommands */
  68.  
  69.  
  70. void EnableModuleCommands (Boolean userWindow)
  71. {
  72. #pragma unused (userWindow)
  73.     MenuHandle    moduleMenu = GetMenuHandle(kModulesMenu);
  74.     WindowPtr    inFront = FrontWindow();
  75.  
  76.     SetItemEnable(moduleMenu, 0, userWindow);
  77.     if (userWindow) {
  78.         int16        item;
  79.         Str255        currModuleName;
  80.         
  81.         GetWTitle(inFront, currModuleName);
  82.         for (item = CountMItems(moduleMenu); item > 0; item--) {
  83.             Str255        currItem;
  84.             
  85.             GetMenuItemText(moduleMenu, item, currItem);
  86.             CheckItem(moduleMenu, item, EqualString(currItem, currModuleName, true, true));
  87.         }
  88.     }
  89. } /* EnableModuleCommands */
  90.  
  91.  
  92. static void ExternalMenuAdjust(DrawingWindowPeek aWindow)
  93. // Find the tool associated with the frontmost window, and
  94. // call its menu adjustment procedure.
  95. {
  96.     if ((gCurrentMenuBar != NULL) && (aWindow->toolRoutines.menuAdjustProc))
  97.         CallToolMenuAdjustProc(aWindow->toolRoutines.menuAdjustProc, (WindowPtr)aWindow);
  98. }
  99.  
  100.  
  101. void UseMenuBar (Handle newMenuBar)
  102. {
  103.     // Install the specified menu bar, or the default menubar if "NULL" is specified
  104.     if (newMenuBar == gCommonMenuBar)    // In case the user passed in the basic menu bar
  105.         newMenuBar = NULL;
  106.     if (newMenuBar != gCurrentMenuBar) {
  107.         if (newMenuBar == NULL)
  108.             SetMenuBar(gCommonMenuBar);
  109.         else
  110.             SetMenuBar(newMenuBar);
  111.         gCurrentMenuBar = newMenuBar;
  112.         DrawMenuBar();
  113.     }
  114. }    
  115.  
  116.  
  117. void AdjustMenus()
  118. {
  119.     enum {
  120.             kNoWindows = 1,
  121.             kUsingDA,
  122.             kUsingDocWindow,
  123.             kUsingSpecialWindow
  124.         };
  125.  
  126.     WindowPtr            inFront = FrontWindow();
  127.     Boolean                isAUserWindow, isDeskAcc;
  128.     int16                newMenuState;
  129.  
  130.     if (inFront == NULL) {
  131.         // No window == turn nearly everything off!
  132.         isAUserWindow = false;
  133.         DisableItem(GetMenuHandle(kEditMenu), 0);
  134.         EnableModuleCommands(false);
  135.         EnableFileCommands(false);
  136.         newMenuState = kNoWindows;
  137.     } else {
  138.         isAUserWindow = isUserWindow(inFront);
  139.         if (isAUserWindow) {
  140.             // We have a module window in front
  141.             EnableFileCommands(true);
  142.             EnableModuleCommands(true);
  143.             DisableItem(GetMenuHandle(kEditMenu), 0);
  144.             ExternalMenuAdjust((DrawingWindowPeek)inFront);
  145.             newMenuState = kUsingDocWindow;
  146.         } 
  147.         else if ((isDeskAcc = ((WindowPeek)inFront)->windowKind < 0) == true) {
  148.             // We have a desk accessory in front
  149.             EnableFileCommands(false);
  150.             EnableModuleCommands(false);
  151.             EnableItem(GetMenuHandle(kEditMenu), 0);
  152.             ExternalMenuAdjust((DrawingWindowPeek)inFront);
  153.             newMenuState = kUsingDA;
  154.         } else {
  155.                // We have a window up front, but it's not a desk accessory or user window
  156.             EnableFileCommands(false);
  157.             EnableModuleCommands(false);
  158.             ExternalMenuAdjust((DrawingWindowPeek)inFront);
  159.             newMenuState = kUsingSpecialWindow;
  160.         }
  161.     }
  162.     
  163.     if (newMenuState != gMenuState) {
  164.         DrawMenuBar();
  165.         gMenuState = newMenuState;
  166.     }
  167. } /* AdjustMenus */
  168.  
  169.  
  170.  
  171. void DoAppleCmds (int16 theItem)
  172. {
  173.     Str255        name;            /* string for DA name    */
  174.  
  175.     switch (theItem) {
  176.  
  177.         case cAbout: 
  178.             Alert(kAboutDialog, NULL);
  179.             break;
  180.             
  181.         default:
  182.             GetMenuItemText(GetMenuHandle(kAppleMenu), theItem, (StringPtr)&name);
  183.             OpenDeskAcc((StringPtr)&name);
  184.     };
  185. }
  186.  
  187. void DoFileCmds (int16 theItem)
  188. {    
  189.     switch (theItem) {
  190.  
  191.         case cNew: 
  192.             NewDisplayWindow();
  193.         break;
  194.                         
  195.         case cClose: 
  196.             CloseAWindow(FrontWindow());
  197.         break;
  198.  
  199.         case cQuit: 
  200.             gDone = true;
  201.         break;
  202.     }
  203. } /* DoFileCmds */
  204.  
  205.  
  206. void DoToolCmds (int16 theItem)
  207. // Locate and load the named tool, unloading any tool that might already be in the window
  208. {    
  209.     Str31        toolName;
  210.     FSSpec        toolSpec;
  211.     OSErr        err;
  212.     WindowPtr    inFront = FrontWindow();
  213.     
  214.     GetMenuItemText(GetMenuHandle(kModulesMenu), theItem, (StringPtr)&toolName);
  215.     err = FindNamedTool (toolName, &toolSpec);
  216.     if (err)
  217.         AlertUser(0, err);
  218.     else {
  219.         DrawingWindowPeek    aWindow = (DrawingWindowPeek)inFront;
  220.         
  221.         if (aWindow) {
  222.             // Unload any tool which might be atatched to the front window
  223.             UnloadTool(inFront);
  224.             // Load the new tool
  225.             err = LoadTool (&toolSpec, inFront);
  226.             if (err == noErr) {
  227.                 SetPort(inFront);
  228.                 InvalRect(&inFront->portRect);
  229.             } 
  230.         }
  231.     }
  232. } /* DoToolCmds */
  233.  
  234.  
  235. static void ExternalMenuDispatch(DrawingWindowPeek aWindow, short theMenu, short theItem)
  236. // Find the tool associated with the frontmost window, and
  237. // call its menu adjustment procedure.
  238. {
  239.     if ((aWindow) && (gCurrentMenuBar != NULL) && (aWindow->toolRoutines.menuDispatchProc))
  240.         CallToolMenuDispatchProc(aWindow->toolRoutines.menuDispatchProc, (WindowPtr)aWindow, theMenu, theItem);
  241. }
  242.  
  243.  
  244. void Dispatch (int32 menuResult)
  245. {
  246.     int16     theMenu = (menuResult >> 16);                    /* menu selected    */
  247.     int16     theItem = (menuResult & 0x0000FFFF);               /* item selected    */
  248.  
  249.     switch (theMenu) {
  250.  
  251.         case kAppleMenu: 
  252.             DoAppleCmds(theItem);
  253.             break;
  254.             
  255.         case kFileMenu: 
  256.             DoFileCmds(theItem);
  257.             break;
  258.             
  259.         case kEditMenu: 
  260.             SystemEdit(theItem - 1);
  261.             break;
  262.                 
  263.         case kModulesMenu:
  264.             DoToolCmds(theItem);
  265.             break;
  266.             
  267.         case kDebugMenu:
  268.             Debugger();
  269.             break;
  270.         
  271.         default:
  272.             ExternalMenuDispatch((DrawingWindowPeek)FrontWindow(), theMenu, theItem);
  273.     }
  274.     HiliteMenu(0);               /* un-hilite selected menu */
  275. }
  276.  
  277. // === Functions which are specific to the ModApp application
  278. static MenuHandle toolMenuHandle;
  279.  
  280. static void InstallNameProc (FSSpec toolFileSpec)
  281. {
  282.     AppendMenu(toolMenuHandle, toolFileSpec.name);
  283. }
  284.  
  285.  
  286. void BuildToolsMenu()
  287. {
  288.     toolMenuHandle = GetMenuHandle(kModulesMenu);
  289.     if (toolMenuHandle) {
  290.         short item;
  291.         
  292.         // Remove all entries from the tools menu
  293.         for (item = CountMItems(toolMenuHandle); item > 0; item--)
  294.             DeleteMenuItem(toolMenuHandle, item);
  295.             
  296.         // Add a list of tool names
  297.         GetToolNames(InstallNameProc);
  298.         
  299.         // No names? Tell the user that this was a failure
  300.         if (CountMItems(toolMenuHandle) == 0) {
  301.             Str255    noToolsString;
  302.             
  303.             GetIndString(noToolsString, rToolMenuStrings, 1);
  304.             AppendMenu(toolMenuHandle, noToolsString);
  305.             DisableItem(toolMenuHandle, 0);
  306.             DisableItem(toolMenuHandle, 1);
  307.         }
  308.     }
  309. }
  310.  
  311.  
  312.